﻿using HospitalFollowUpSystem.DBC;
using HospitalFollowUpSystem.Entity;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HospitalFollowUpSystem.View
{
    //TASK 各类 快捷键 
    public partial class FrmMain : MetroForm
    {
        //所有控件的集合
        List<Control> controls = new List<Control>();
        //随访任务列表
        List<OutPatient> outPatientList = new List<OutPatient>();
        //dgv当前位置
        int dgvIndex = 0;

        public FrmMain()
        {
            InitializeComponent();
            //初始化
            InitView();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 界面初始化
        /// 数据写入
        /// </summary>
        private void InitView()
        {
            GetAllControls(this);
            //遍历Form上的所有控件-给Label按钮添加动效  
            foreach (Control control in controls)
            {
                Console.WriteLine(control.Name + " " + control.Text);
                if (control is Label)
                {
                    if (control.Name.IndexOf("labBtn") != -1)
                    {
                        control.MouseUp += new MouseEventHandler(LabelButtonMouseUp);
                        control.MouseDown += new MouseEventHandler(LabelButtonMouseDown);
                    }
                }
            }
            GetAllControls(this);
            //遍历Form上的所有控件-给Label按钮添加动效  
            foreach (Control control in controls)
            {
                Console.WriteLine(control.Name + " " + control.Text);
                if (control is Label)
                {
                    if (control.Name.IndexOf("labBtn") != -1)
                    {
                        control.MouseUp += new MouseEventHandler(LabelButtonMouseUp);
                        control.MouseDown += new MouseEventHandler(LabelButtonMouseDown);
                    }
                }
            }

            HIS_DB db = new HIS_DB();
            DateTime startDate = Convert.ToDateTime("2017-12-06");
            DateTime endDate = Convert.ToDateTime("2017-12-08");
            outPatientList = db.ReadDischargedPatient(startDate, endDate);
            db.Database.Close();
            //
            dgvOutPatient.DataSource = outPatientList;
            //
            dgvOutPatient.Columns["Id"].HeaderText = "";
            dgvOutPatient.Columns["Name"].HeaderText = "姓名";
            dgvOutPatient.Columns["Sex"].HeaderText = "性别";
            dgvOutPatient.Columns["Birthday"].HeaderText = "生日";
            dgvOutPatient.Columns["IdCard"].HeaderText = "身份证";
            dgvOutPatient.Columns["TelNum"].HeaderText = "电话号码";
            dgvOutPatient.Columns["NowAddress"].HeaderText = "住址";
            dgvOutPatient.Columns["InDate"].HeaderText = "";
            dgvOutPatient.Columns["InKSName"].HeaderText = "入院科室";
            dgvOutPatient.Columns["InKSCode"].HeaderText = "";
            dgvOutPatient.Columns["OutDate"].HeaderText = "出院时间";
            dgvOutPatient.Columns["OutKSName"].HeaderText = "出院科室";
            dgvOutPatient.Columns["OutKSCode"].HeaderText = "";

            var ColumnCount = dgvOutPatient.Columns.Count;
            for (var i = 0; i < ColumnCount; i++)
            {
                dgvOutPatient.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
                if (dgvOutPatient.Columns[i].HeaderText == "")
                {
                    dgvOutPatient.Columns[i].Visible = false;
                }
            }
            dgvOutPatient.Columns[ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            WashFace(null);
        }

        /// <summary>
        /// 查看随访任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labBtnFollowTask_Click(object sender, EventArgs e)
        {
            //
        }

        /// <summary>
        /// 患者疾病信息变更
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labBtnPatientInfo_Click(object sender, EventArgs e)
        {
            Form frm = new FrmPatientInfoUpDate();
            frm.ShowDialog();
        }

        /// <summary>
        /// 模板管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labBtnTemplate_Click(object sender, EventArgs e)
        {
            Form frm = new FrmAddTopicType();
            frm.ShowDialog();
        }

        /// <summary>
        /// 刷新数据-洗脸
        /// </summary>
        private void WashFace(OutPatient outPatient)
        {
            //获得labXXXX/用于存放内容的label标签
            foreach (Control control in controls)
            {
                if (control is Label)
                {
                    if (control.Name.IndexOf("labBtn") == -1 && control.Name.IndexOf("label") == -1)
                    {
                        Console.WriteLine(control.Name);
                        control.Text = "选择随访对象";
                    }
                }
            }

            //显示短信和问卷的状态
            labTableStatus.Text = "未提交";
            labMSMStatus.Text = "未发送";

            //短信内容
            txtMSMContent.Text = "";

            //赋值
            if (!(outPatient is null))
            {
                //FIX 判断状态   
                labTableStatus.Text = "已提交";//"未提交"
                labMSMStatus.Text = "已发送";//"未发送"
                //FIX 诊断需要加上
                labMainDiagnose.Text = "没有诊断怎么行";

                labOutKSName.Text = outPatient.OutKSName;
                labInKSName.Text = outPatient.InKSName;
                labOutKSDate.Text = outPatient.OutDate.ToString("yyyy-MM-dd");
                labInKSDate.Text = outPatient.InDate.ToString("yyyy-MM-dd");
                labNowAddress.Text = outPatient.NowAddress;
                labTelNum.Text = outPatient.TelNum;
                labIDCard.Text = outPatient.IDCard;
                labBirthday.Text = outPatient.Birthday.ToString("yyyy-MM-dd");
                labSex.Text = outPatient.Sex;
                labPatientName.Text = outPatient.Name;
                labPatientName2.Text = labPatientName.Text;
            }

            //FIX 赋值短信内容

            //FIX 赋值问卷内容
        }





        //以下为工具方法//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





        /// <summary>
        /// Label按钮鼠标点下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LabelButtonMouseDown(object sender, MouseEventArgs e)
        {
            if (sender is ToolStripLabel)
            {
                ((ToolStripLabel)sender).ForeColor = Color.White;
            }

            if (sender is Label)
            {
                ((Label)sender).ForeColor = Color.White;
            }
        }

        /// <summary>
        /// Label按钮鼠标放开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LabelButtonMouseUp(object sender, MouseEventArgs e)
        {
            if (sender is ToolStripLabel)
            {
                ((ToolStripLabel)sender).ForeColor = Color.Black;
            }

            if (sender is Label)
            {
                ((Label)sender).ForeColor = Color.Black;
            }
        }

        /// <summary>
        /// 获取窗口所有控件-包括子空间-使用递归
        /// </summary>
        /// <param name="control"></param>
        private void GetAllControls(Control control)
        {
            foreach (Control con in control.Controls)
            {
                controls.Add(con);
                if (con.Controls.Count > 0)
                {
                    GetAllControls(con);
                }
            }
        }

        /// <summary>
        /// 随访任务列表 选择任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvOutPatient_DoubleClick(object sender, EventArgs e)
        {
            dgvIndex = dgvOutPatient.CurrentRow.Index;
            OutPatient outPatient = outPatientList[dgvIndex];
            WashFace(outPatient);
        }

        /// <summary>
        /// 上一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labBtnLastOne_Click(object sender, EventArgs e)
        {
            if (dgvIndex == 0)
            {
                OutPatient outPatient = outPatientList[0];
                WashFace(outPatient);
                MessageBox.Show("已经是第一个", "操作提醒");
            }
            else
            {
                dgvIndex--;
                OutPatient outPatient = outPatientList[dgvIndex];
                WashFace(outPatient);
            }

            dgvOutPatient.Rows[dgvIndex].Selected = true;
        }

        /// <summary>
        /// 下一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labBtnNextOne_Click(object sender, EventArgs e)
        {
            if (dgvIndex < outPatientList.Count - 1)
            {
                dgvIndex++;
                OutPatient outPatient = outPatientList[dgvIndex];
                WashFace(outPatient);
            }
            else
            {
                OutPatient outPatient = outPatientList[outPatientList.Count - 1];
                WashFace(outPatient);
                MessageBox.Show("您今天的工作做完了", "操作提醒");

            }

            dgvOutPatient.Rows[dgvIndex].Selected = true;
        }

        private void labNowAddress_Click(object sender, EventArgs e)
        {
            MessageBox.Show(labNowAddress.Text, "如果地址太长");
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    labBtnSMSTemplate_Click(sender, e);
                    break;
                case Keys.F2:
                    labBtnSubSMS_Click(sender, e);
                    break;
                case Keys.F3:
                    labBtnSaveSMSTemplate_Click(sender, e);
                    break;
                case Keys.F4:
                    labBtnSelectTable_Click(sender, e);
                    break;
                case Keys.F5:
                    labBtnCall_Click(sender, e);
                    break;
                case Keys.F6:
                    labBtnSubTable_Click(sender, e);
                    break;
                case Keys.F7:
                    labBtnMarkErrTel_Click(sender, e);
                    break;
            }
        }

        private void labBtnSMSTemplate_Click(object sender, EventArgs e)
        {
            MessageBox.Show("打开模板");
        }

        private void labBtnSubSMS_Click(object sender, EventArgs e)
        {
            MessageBox.Show("发送信息");
        }

        private void labBtnSaveSMSTemplate_Click(object sender, EventArgs e)
        {
            MessageBox.Show("另存模板");
        }

        private void labBtnSelectTable_Click(object sender, EventArgs e)
        {
            MessageBox.Show("选择问卷");
        }

        private void labBtnCall_Click(object sender, EventArgs e)
        {
            MessageBox.Show("拨打电话");
        }

        private void labBtnSubTable_Click(object sender, EventArgs e)
        {
            MessageBox.Show("提交问卷");
        }

        private void labBtnMarkErrTel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("联系方式-失效标注");
        }
    }
}
